<?php

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

namespace EasyActionPHP\Dispatcher;

use EasyActionPHP\Controller as Controller;

/**
 * Classe responsável pela execução e mais baixo nivel de uma ação.
 *
 * @author Flavio de Vasconcellos Correa
 * @since 2007/06/04
 * @package EasyActionPHP
 * @subpackage Dispatcher
 * @version $id$
 * @license http://www.gnu.org/copyleft/gpl.html GPL
 */
class ActionDispatcher {

    /**
     * Processa a resposta vinda de uma ação.
     *
     * @access public
     * @param ActionForward forward
     * @param HttpResponse response
     * @return void
     */
    public static function execute(Controller\ActionForward $forward, Controller\HttpResponse &$response) {
        try {
            if ($forward->redirect) {
                if (substr($forward->path, 0, 1) == '?') {
                    $url = self::getCurrentUrl() . $forward->path;
                    foreach ($forward->gets as $key => $value) {
                        $url .= '&' . $key . '=' . $value;
                    }
                } else {
                    $url = $forward->path;
                }
                self::redirect($url);
            } elseif ($forward->fetch) {
                return ActionTemplateSingleton::getInstance()->fetch($forward->path, $response);
            } else {
                return ActionTemplateSingleton::fetch($forward->path, $response);
//				ActionTemplateSingleton::getInstance()->display($forward->path, $response);
            }
        } catch (\Exception $e) {
            throw ($e);
        }
        return 0;
    }

    /**
     * Retorna a URL que fez a chamada.
     *
     * @return String URL
     */
    public function getCurrentUrl() {
        return 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/';
    }

    /**
     * Retorna a URI que fez a chamada.
     *
     * @return String URI
     */
    public static function getCurrentUri() {
        $pageURL = 'http';
        if ($_SERVER['HTTPS'] == 'on') {
            $pageURL .= "s";
        }
        $pageURL .= '://';
        if ($_SERVER['SERVER_PORT'] != '80') {
            $pageURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
        } else {
            $pageURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
        }
        return $pageURL;
    }

    /**
     * Refaz uma entrada associado a respostas da aplicação.
     *
     * @param String input
     * @param HttpResponse response
     * @access public
     * @return void
     */
    public function reloadInput($input, HttpResponse &$response) {
        $template = ActionTemplateSingleton::getInstance();
        $template->display($input, $response);
        exit();
    }

    /**
     * Redireciona em maior baixo nível para uma URL específica.
     *
     * @param String url
     * @access public
     * @return void
     */
    public function redirect($url) {
        Header('Location:' . $url);
        exit();
    }

    /**
     * Dispara somente Forwards Globais
     *
     * @param String name Nome que identifica uma tag <code><forward></code> em <code><global-forwards></code>.
     * @access public
     * @return void
     */
    public function forward($name, Controller\HttpResponse &$response) {
        $forward = Controller\ActionController::getGlobalForward($name);
        if ($forward) {
            self::execute($forward, $response);
        }
    }

}

?>